package com.applink.sell_proof.database.table;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingDatabaseHelper;
import com.applink.sell_proof.model.Answer;
import com.applink.sell_proof.model.Question;

public class QuestionDBHelper {
	public static final String TABLE_NAME = "question_table";
	public static final String QUESTION_ID = "question_id";
	public static final String QUESTION_CONTENT = "content";
	public static final String QUIZ_ID = "quiz_id";
	public static final String ANSWER_ID = "anwser_id";
	public static final String USER_REPLY = "user_reply";
	public static final String ANSWER_TYPE = "answer_type";
	public static final String OPTIONS = "answer_options";
	public static final String RESULT = "answer_result";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ QUESTION_ID + " integer not null ," + QUESTION_CONTENT
			+ " text ," + QUIZ_ID + " integer not null ," + ANSWER_ID
			+ " integer not null ," + USER_REPLY + " text ," + ANSWER_TYPE
			+ " text ," + OPTIONS + " text ," + RESULT + " text ,"
			+ "PRIMARY KEY" + "(" + QUESTION_ID + "," + QUIZ_ID + ","
			+ ANSWER_ID + ")" + ");";

	public static void createQuestion(long quizId, long questionId,
			long answerId, String questionContent, String userReply,
			String options, String type) {

		ContentValues value = new ContentValues();
		value.put(QUESTION_ID, questionId);
		value.put(QUESTION_CONTENT, questionContent);
		value.put(QUIZ_ID, quizId);
		value.put(ANSWER_ID, answerId);
		value.put(USER_REPLY, userReply);
		value.put(OPTIONS, options);
		value.put(ANSWER_TYPE, type);
		SQLiteOpenHelper dbHelper = new EMarketingDatabaseHelper(EMarketing
				.getInstance().getApplicationContext());
		SQLiteDatabase db = dbHelper.getWritableDatabase();

		db.insert(TABLE_NAME, null, value);
		db.close();
	}

	public static Question getQuestion(long questionId, long quizId) {
		SQLiteOpenHelper dbHelper = new EMarketingDatabaseHelper(EMarketing
				.getInstance().getApplicationContext());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db
				.query(TABLE_NAME,
						new String[] { QUESTION_ID, ANSWER_ID, USER_REPLY,
								QUESTION_CONTENT, ANSWER_TYPE, OPTIONS, RESULT },
						QUIZ_ID + "=?" + " AND " + QUESTION_ID + "=?",
						new String[] { Long.toString(quizId),
								Long.toString(questionId) }, null, null, null);
		try {
			if (cursor != null && cursor.moveToFirst()) {
				Question question = new Question();
				String content = cursor.getString(cursor
						.getColumnIndex(QUESTION_CONTENT));
				question.setContent(content);
				String userReply = cursor.getString(cursor
						.getColumnIndex(USER_REPLY));
				String answerType = cursor.getString(cursor
						.getColumnIndex(ANSWER_TYPE));
				String options = cursor.getString(cursor
						.getColumnIndex(OPTIONS));
				String result = cursor.getString(cursor.getColumnIndex(RESULT));
				long answerId = cursor
						.getLong(cursor.getColumnIndex(ANSWER_ID));
				Answer answer = new Answer(answerId, options, answerType,
						userReply, result);
				Log.e("get question Quiz", "quizId " + quizId + " questionId "
						+ questionId + " answerId " + answerId + " result "
						+ result);
				question.addAnswer(answer);

				while (cursor.moveToNext()) {
					userReply = cursor.getString(cursor
							.getColumnIndex(USER_REPLY));
					answerType = cursor.getString(cursor
							.getColumnIndex(ANSWER_TYPE));
					answerId = cursor.getLong(cursor.getColumnIndex(ANSWER_ID));
					options = cursor.getString(cursor.getColumnIndex(OPTIONS));
					result = cursor.getString(cursor.getColumnIndex(RESULT));
					Log.e("get question Quiz", "quizId " + quizId
							+ " questionId " + questionId + " answerId "
							+ answerId + " result " + result);
					answer = new Answer(answerId, options, answerType,
							userReply, result);
					question.addAnswer(answer);
				}

				return question;
			}
		} finally {
			cursor.close();
			db.close();
		}
		return null;
	}

	public static List<Long> getListQuestionIdInQuiz(long quizId) {
		SQLiteOpenHelper dbHelper = new EMarketingDatabaseHelper(EMarketing
				.getInstance().getApplicationContext());

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		SQLiteQueryBuilder queryBuildre = new SQLiteQueryBuilder();
		queryBuildre.setDistinct(true);
		queryBuildre.setTables(TABLE_NAME);
		Cursor cursor = queryBuildre.query(db, new String[] { QUESTION_ID },
				QUIZ_ID + "=?", new String[] { Long.toString(quizId) }, null,
				null, null);
		try {
			if (cursor != null && cursor.moveToFirst()) {
				List<Long> questionIds = new ArrayList<Long>();
				questionIds.add(cursor.getLong(cursor
						.getColumnIndex(QUESTION_ID)));
				while (cursor.moveToNext()) {
					questionIds.add(cursor.getLong(cursor
							.getColumnIndex(QUESTION_ID)));
				}
				return questionIds;
			}
		} finally {
			cursor.close();
			db.close();
		}
		return null;
	}

	public static void updateUserReply(final long answerId, final String reply) {
		new Thread() {
			@Override
			public void run() {
				SQLiteOpenHelper dbHelper = new EMarketingDatabaseHelper(
						EMarketing.getInstance().getApplicationContext());
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				ContentValues values = new ContentValues();
				values.put(USER_REPLY, reply);
				try {
					db.update(TABLE_NAME, values, ANSWER_ID + "=?",
							new String[] { Long.toString(answerId) });
				} finally {
					db.close();
				}
			};
		}.start();

	}

	public static Map<Long, String> getAllAnswerIdAndUserReplyInQuiz(Long quizId) {
		SQLiteOpenHelper dbHelper = new EMarketingDatabaseHelper(EMarketing
				.getInstance().getApplicationContext());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		SQLiteQueryBuilder queryBuildre = new SQLiteQueryBuilder();
		queryBuildre.setDistinct(true);
		queryBuildre.setTables(TABLE_NAME);
		Cursor cursor = queryBuildre.query(db, new String[] { ANSWER_ID,
				USER_REPLY }, QUIZ_ID + "=?",
				new String[] { Long.toString(quizId) }, null, null, null);
		Map<Long, String> mapAnswer = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				mapAnswer = new HashMap<Long, String>();
				Long answerId = cursor
						.getLong(cursor.getColumnIndex(ANSWER_ID));
				String userReply = cursor.getString(cursor
						.getColumnIndex(USER_REPLY));
				mapAnswer.put(answerId, userReply);
				while (cursor.moveToNext()) {
					answerId = cursor.getLong(cursor.getColumnIndex(ANSWER_ID));
					userReply = cursor.getString(cursor
							.getColumnIndex(USER_REPLY));
					mapAnswer.put(answerId, userReply);
				}
			}
		} finally {
			cursor.close();
			db.close();
		}
		return mapAnswer;
	}

	public static void updateAnswerResult(long quizId, long questionId,
			long answerId, String result) {
		Log.e("Quiz", "quizId " + quizId + " questionId " + questionId
				+ " answerId " + answerId + " result " + result);
		
		SQLiteOpenHelper dbHelper = new EMarketingDatabaseHelper(EMarketing
				.getInstance().getApplicationContext());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(RESULT, result);
		try {
			db.update(
					TABLE_NAME,
					values,
					ANSWER_ID + "=?" + " AND " + QUIZ_ID + "=?" + " AND "
							+ QUESTION_ID + "=?",
					new String[] { Long.toString(answerId),
							Long.toString(quizId), Long.toString(questionId) });
		} finally {
			db.close();
		}
	}
}
